package com.bl.student.service.biz.point.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.PointType;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.model.custom.UserPointEntity;
import com.bl.student.dao.repository.*;
import com.bl.student.service.biz.point.PointService;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.vo.order.OrderModVo;
import com.bl.student.service.vo.point.PointListVo;
import com.bl.student.service.vo.point.PointModVo;
import com.bl.student.service.vo.point.PointTypeListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/8/5.
 */
@Service
@Log4j2
public class PointServiceImpl implements PointService {


    @Autowired
    UserPointRepository userPointRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    InviteRepository inviteRepository;

    @Autowired
    PointRepository pointRepository;

    @Autowired
    UserService userService;


    /*
    增加用户积分记录，通用方法
     */
    @Override
    public void addUserPoint(String userId, PointType type, int point, String isOut) {
        UserPointEntity userPointEntity = new UserPointEntity();
        userPointEntity.setCount(point);
        userPointEntity.setCreateTime(new Date());
        userPointEntity.setId(UUIDUtils.getUUID());
        userPointEntity.setType(type);
        userPointEntity.setUpdateTime(new Date());
        userPointEntity.setUserId(userId);
        userPointEntity.setIsOut(isOut);

        userPointRepository.save(userPointEntity);
    }

    @Override
    public void addTeacherPoint(String teacherId, PointType type, int point, String isOut) {
        UserPointEntity userPointEntity = new UserPointEntity();
        userPointEntity.setCount(point);
        userPointEntity.setCreateTime(new Date());
        userPointEntity.setId(UUIDUtils.getUUID());
        userPointEntity.setType(type);
        userPointEntity.setUpdateTime(new Date());
        userPointEntity.setUserId(teacherId);
        userPointEntity.setIsOut(isOut);

        userPointRepository.save(userPointEntity);
    }

    /**
     * 积分增加 乙方
     *
     * @param userId
     * @param point
     */
    @Override
    public void addPoint(String userId, int point) {
        UserEntity userEntity = userRepository.findOne(userId);

        userEntity.setUpdateTime(new Date());

        log.error("userEntity.getPoint()" + userEntity.getPoint());
        userEntity.setPoint(userEntity.getPoint() + point);
        userRepository.save(userEntity);
    }

    /**
     * 积分增加 甲方
     *
     * @param teacherId
     * @param point
     */
    @Override
    public void addPointPartyA(String teacherId, int point) {
        TeacherEntity teacherEntity = teacherRepository.findOne(teacherId);

        teacherEntity.setUpdateTime(new Date());

        log.error("userEntity.getPoint()" + teacherEntity.getPoint());
        teacherEntity.setPoint(teacherEntity.getPoint() + point);
        teacherRepository.save(teacherEntity);
    }

    /**
     * 减少积分 乙方
     *
     * @param userEntity
     * @param point
     */
    @Override
    public void delPoint(UserEntity userEntity, int point) {
        userEntity.setPoint(userEntity.getPoint() - point);
        userEntity.setUpdateTime(new Date());
        userRepository.save(userEntity);

        // 同时减少甲方积分
        List<TeacherEntity> teacherEntityList = teacherRepository.findByOpenId(userEntity.getOpenId());
        TeacherEntity teacherEntity = ListUtils.getFirst(teacherEntityList);
        teacherEntity.setPoint(teacherEntity.getPoint() - point);
        teacherEntity.setUpdateTime(new Date());
        teacherRepository.save(teacherEntity);
    }

    /**
     * 减少积分甲方
     *
     * @param teacherEntity
     * @param point
     */
    @Override
    public void delPointPartyA(TeacherEntity teacherEntity, int point) {
        teacherEntity.setPoint(teacherEntity.getPoint() - point);
        teacherEntity.setUpdateTime(new Date());
        teacherRepository.save(teacherEntity);

//        // 同时减少乙方积分
        UserEntity userEntity = userService.getUserByOpenId(teacherEntity.getOpenId());
        userEntity.setPoint(userEntity.getPoint() - point);
        userEntity.setUpdateTime(new Date());
        userRepository.save(userEntity);
    }

    /**
     * userid 不区分 甲方乙方
     * @param userId
     * @param type
     * @param pageable
     * @return
     */
    @Override
    public PointListVo getPointList(String userId, String type, Pageable pageable) {

        PointListVo pointListVo = new PointListVo();
        Page<UserPointEntity> userPointEntities = null;
        if ("IN".equals(type)) {
            userPointEntities = userPointRepository.findByUserIdAndIsOutOrderByCreateTimeDesc(userId, "0", pageable);
        } else if("OUT".equals(type)) {
            userPointEntities = userPointRepository.findByUserIdAndIsOutOrderByCreateTimeDesc(userId, "1", pageable);
        } else  {
            userPointEntities = userPointRepository.findByUserIdOrderByCreateTimeDesc(userId,pageable);
        }

        pointListVo.setTotalCount(userPointEntities.getTotalElements());
        pointListVo.setList(userPointEntities.getContent());

        return pointListVo;
    }

    @Override
    public void mod(PointModVo pointModVo) {
        PointEntity pointEntity = pointRepository.findOne(pointModVo.getId());
        BeanUtils.copyProperties(pointModVo, pointEntity);
        pointRepository.save(pointEntity);
    }

    @Override
    public PointTypeListVo getPointTypeList() {

        PointTypeListVo pointTypeListVo = new PointTypeListVo();
        List<PointEntity> pointEntities = new ArrayList<>();
        pointEntities = pointRepository.findAll();
        pointTypeListVo.setTotalCount(pointEntities.size());
        pointTypeListVo.setList(pointEntities);

        return pointTypeListVo;
    }




}
